package ee.greaton.kultuuriaken;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
	private Context context;
	private EventList eventList;
	private boolean withDate;

	public MyAdapter(Context context, EventList eventList, boolean withDate) {
		this.context = context;
		this.eventList = eventList;
		this.withDate = withDate;
	}

	public int getCount() {
		return eventList.size();
	}

	public Object getItem(int position) {
		return eventList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup viewGroup) {
		Event entry = eventList.get(position);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			 if(Config.theme == 0){
				 convertView = inflater.inflate(R.layout.tume_row, null);
			 }else{
				 convertView = inflater.inflate(R.layout.hele_row, null);
			 }
		}
		TextView titleTv = (TextView) convertView
				.findViewById(R.id.title);
		titleTv.setText(entry.getTitle());
		
		if(withDate){
			TextView timeTv = (TextView) convertView.findViewById(R.id.titleTime);
			timeTv.setText(getShortDate(entry.getDate()));
			
			timeTv.setTextColor(Color.parseColor("#8CB916"));
			
			TextView dateTv = (TextView) convertView.findViewById(R.id.titleDate);
			dateTv.setText(entry.getTime());
		}else{
			TextView timeTv = (TextView) convertView.findViewById(R.id.titleTime);
			timeTv.setText(entry.getTime());
			//timeTv.setTextColor(R.color.valge);
		}
		return convertView;
	}
	
	private String getShortDate(String date){
		if(date.indexOf(".") != -1){
			String[] dateParts = date.split("\\.");
			return dateParts[2] + "." + dateParts[1];
		}else{
			return date;
		}
	}
}
